package org.UDPDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class receiveMessageDemo {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket=new DatagramSocket(10086);
        DatagramPacket datagramPacket=new DatagramPacket(new byte[1024],1024);
        datagramSocket.receive(datagramPacket);
        byte[] data = datagramPacket.getData();
        int port = datagramPacket.getPort();
        InetAddress address = datagramPacket.getAddress();
        int length = datagramPacket.getLength();
        String  str=new String(data,0,length);
        System.out.println("UDP接收数据案例");
        System.out.println("结果：从ip地址"+address+"的端口"+port+"获取到了数据："+str);
        datagramSocket.close();

    }
}
